from peewee import *

db = MySQLDatabase('test',
                   **{'charset': 'utf8', 'sql_mode': 'PIPES_AS_CONCAT', 'use_unicode': True, 'user': 'root',
                      'password': 'root'})


class UnknownField(object):
    def __init__(self, *_, **__): pass


class BaseModel(Model):
    class Meta:
        database = db


class User(BaseModel):
    age = IntegerField(null=True)
    birthday = DateTimeField(null=True)
    name = CharField(null=True)

    class Meta:
        table_name = 'user'


# 批量插入元组
from random import randint

data = [
    (f'王五{i}号', randint(20, 40), f'{randint(1980, 2010)}-{randint(1, 13)}-{randint(1, 29)}')
    for i in range(10000)
]

"""
Depending on the number of rows in your data source, you may need to break it up into chunks. 
SQLite in particular typically has a limit of 999 or 32766 variables-per-query (batch size would then be 999 // row length or 32766 // row length).
"""

# You can write a loop to batch your data into chunks (in which case it is strongly recommended you use a transaction):
# 每次插入100条数据
with db.atomic():
    for idx in range(0, len(data), 100):
        User.insert_many(data[idx:idx + 100], fields=['name', 'age', 'birthday']).execute()
